﻿using System;
using System.Data;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using GUI.Extensions;

namespace GUI.Controls.Sport
{
    public partial class MostViewNews : System.Web.UI.UserControl
    {
        public bool AutoDataBind = true;
        private const int Top = 10;
        private const string LiFormat = "<li>{3}<p><a href=\"{0}\" title=\"{2}\">{1}</a></p></li>";
        private const int ImageWidth = 80;
        private const int ImageHeight = 60;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (AutoDataBind)
            {
                var objCat = RequestExtensions.Category;
                if (objCat!= null)
                {
                    var dt = NewsBO.GetInstance().GetMostViewNews(Top, objCat.Id);
                    Visible = DataBind(dt);
                    return;
                }
                Visible = false;
            }
        }

        public bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new System.Text.StringBuilder();

                foreach (DataRow r in dt.Rows)
                {
                    var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth, ImageHeight, lazyLoad: true);

                    sb.Append(string.Format(
                            LiFormat,
                            r[SqlConstants.NewsPublish.Url],
                            r[SqlConstants.NewsPublish.Title],
                            r[SqlConstants.NewsPublish.EncodedTitle],
                            avatar
                        ));

                }
                ltrList.Text = sb.ToString();
                return true;
            }
            return false;
        }
    }
}